package com.tanhua.server.controller;


import com.tanhua.domain.db.Settings;
import com.tanhua.server.Interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
@Slf4j
public class SettingsController {

    @Autowired
    private SettingsService settingsService;
    @Autowired
    private UserService userService;


    /**
     *接口名称：用户通用设置 - 读取
     *接口路径：GET /users/settings
     *
     */
    @GetMapping("/settings")
    public ResponseEntity querySettings(){
        return settingsService.querySettings();
    }

    /**
     *接口名称：通知设置 - 保存
     *接口路径：POST/users/notifications/setting
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity saveNotification(@RequestBody  Settings settings){
        return settingsService.saveNotification(settings);
    }


    /**
     *接口名称：设置陌生人问题 - 保存
     * 接口路径：POST/users/questions
     */
    @PostMapping("/questions")
    public ResponseEntity saveQuestion(@RequestBody Map<String,String> param){
        String content = param.get("content");
        return settingsService.saveQuestion(content);
    }


    /**
     *接口名称：黑名单 - 翻页列表
     *接口路径：GET/users/blacklist
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        return settingsService.blacklist(page,pagesize);
    }


    /**
     *接口名称：黑名单 - 移除
     * 接口路径：DELETE /users/blacklist/:uid
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackUser(@PathVariable("uid") Long blackUserId){
        return settingsService.deleteBlackUser(blackUserId);
    }

    /*-----------------------修改手机号----------------------------------------*/

    /**
     * 接口名称：修改手机号-发送短信验证码
     * 接口路径：POST/users/phone/sendVerificationCode
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity<Object> sendVerificationCode(){
        //获取登录用户手机号码，发送验证码
        String phone = UserHolder.get().getMobile();
        return userService.login(phone);
    }

    /**
     * 接口名称：修改手机号 - 校验验证码
     * 接口路径：POST/users/phone/checkVerificationCode
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity<Object> checkVerificationCode(@RequestBody Map<String, String> map){
        //获取验证码
        String code = map.get("verificationCode");
        return userService.checkVerificationCode(code);
    }

    /**
     * 接口名称：修改手机号 - 保存
     * 接口路径：POST/users/phone
     */
    @PostMapping("/phone")
    public ResponseEntity<Object> phone(@RequestBody Map<String, String> map){
        //获取手机号
        String phone = map.get("phone");
        return userService.updateUserPhone(phone);
    }
}
